package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.UserPermissionDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.Permission;
import com.itheima.health.service.UserPermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;

/**
 * @author ：lizimo
 * @date ：Created in 2020/1/2
 * @description ：用户权限实现类
 * @version: 1.0
 */
@Slf4j
@Service
public class UserPermissionServiceImpl implements UserPermissionService {

    @Autowired
    private UserPermissionDao userPermissionDao;

    @Transactional
    @Override
    public void add(Permission permission) {
        log.info("[权限-添加]data：{}",permission);
        userPermissionDao.insert(permission);

    }
    @Transactional
    @Override
    public void delete(Integer id) {
        log.info("[权限-删除]data:{}",id);

        Long count = userPermissionDao.countRoleByPermissionId(id);
        if (count>0){
            throw new BusinessRuntimeException("当前权限有数据，不能删除");
        }
        //实际删除操作
        userPermissionDao.delete(id);
    }
    @Transactional
    @Override
    public void edit(Permission permission) {
        log.info("[权限-编辑]data:{}",permission);
        userPermissionDao.update(permission);

    }

    @Override
    public Permission findById(Integer id) {
        log.info("[权限-根据Id查询]data:{}",id);

        return userPermissionDao.selectById(id);
    }

    @Override
    public List<Permission> findAll() {
        log.info("[权限-查询所有]");

        return userPermissionDao.selectAll();
    }

    @Override
    @GetMapping("findPage")
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        //使用分页插件
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //分页查询数据库
        Page<Permission> page = userPermissionDao.selectByConditon(queryPageBean.getQueryString());

        return new PageResult(page.getTotal(),page.getResult());
    }

}
